import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/controllers/device/dail/dial_logic.dart';
import 'package:share_fit/app/controllers/device/dail/dial_model.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/app/tools/log_utils.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'dial_const.dart';

class DialShowDialog extends Dialog {

  final logic = Get.put(DialLogic());
  final state = Get.find<DialLogic>().state;
  
  final DialModel dialModel;

  DialShowDialog(this.dialModel, {super.key});

  @override
  Widget build(BuildContext context) {
    state.uploadFileValue = 0;
    state.uploadFileText = '';
    state.isUploadFile = false;
    return GestureDetector( // 点击遮罩层隐藏弹框
        child: Material(
            type: MaterialType.transparency, // 配置透明度
            child: Center(
                child: GestureDetector( // 点击遮罩层关闭弹框，并且点击非遮罩区域禁止关闭
                    onTap: () {
                      logger.d('～');
                    },
                    child: Container(
                        width: 900.w,
                        height: 1300.w,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          color: Colors.white,
                        ),
                        child: Column(
                            children: <Widget>[
                              Container(
                                  alignment: Alignment.topCenter,
                                  margin: EdgeInsets.fromLTRB(0, 50.h, 0, 0),
                                  child: Text(
                                      dialModel.title,
                                      style: TextStyle(
                                        fontSize: 54.sp,
                                        color: ColorConst.btnFontColor,
                                      )
                                  )
                              ),
                              Container(
                                  alignment: Alignment.topCenter,
                                  child: Text(
                                      '1k+ ${LocaleKeys.dial_download_num.ts}',
                                      style: TextStyle(
                                        fontSize: 46.sp,
                                        color: ColorConst.secondaryFontColor,
                                      ))),
                              Image(image: NetworkImage(dialModel.preview), width: 600.w, height: 800.w,),
                              Container(
                                width: 700.w,
                                height: 140.h,
                                margin: EdgeInsets.only(top: 50.h),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  color: ColorConst.gray50,
                                  borderRadius: BorderRadius.circular(30)),
                                child: Stack(
                                  children: [
                                    GetBuilder<DialLogic>(
                                      id: DialConst.dialUploadFileViewId,
                                      builder: (_){
                                        return SizedBox(
                                          width: 700.w,
                                          height: 140.h,
                                          child: ClipRRect(
                                            borderRadius: const BorderRadius.all(Radius.circular(100)),
                                            child: LinearProgressIndicator(
                                              value: state.uploadFileValue,
                                              backgroundColor: ColorConst.gray50,
                                              valueColor: AlwaysStoppedAnimation<Color>(ColorConst.accentColor),
                                            ),),);
                                      }),
                                    GestureDetector(
                                      behavior: HitTestBehavior.opaque,
                                      onTap: (){
                                        // if(!state.hasPermisson) {
                                        //   showToast(LocaleKeys.dial_open_permisson.ts);
                                        //   return;
                                        // }
                                        logic.download(dialModel.resource).then((value) =>
                                            logic.uploadWatchFace(dialModel.resource).then((value){
                                              bool isSame = false;
                                              List<dynamic> lists = state.downlodLists;
                                              for(var element in lists) {
                                                DialModel temp = DialModel().fromJson(jsonDecode(element));
                                                isSame = temp.title == dialModel.title;
                                                if(temp.title == dialModel.title) {
                                                  isSame = true;
                                                  break;
                                                }
                                              }

                                              logic.getWatchFaceInfo().then((value) {
                                                if(state.watchFaceInfo.isNotEmpty) {
                                                  var info = state.watchFaceInfo[state.watchFaceInfo.length-1];
                                                  Map map = jsonDecode(jsonEncode(info));
                                                  if((4&map['type'])==4){
                                                    dialModel.bleId = '${map['id']}';
                                                  }
                                                }

                                                logger.d('isSame:$isSame');
                                                if(!isSame) {
                                                  state.downlodLists.add(dialModel.toJson());
                                                }
                                                logic.save('download_lists', lists);

                                                logger.d(state.downlodLists);
                                              });
                                              Future.delayed(const Duration(seconds: 1), ()=>Navigator.pop(context));
                                            }));
                                      },
                                      child: Center(
                                        child: GetBuilder<DialLogic>(
                                          id: DialConst.dialUploadFileTextViewId,
                                          builder: (_){
                                            return Text(state.uploadFileText.isEmpty?LocaleKeys.dial_sync.ts:state.uploadFileText, style: TextStyle(fontSize: 46.sp, color: ColorConst.btnFontColor),);
                                          },
                                        ),
                                      ),),
                                  ],
                                ),
                              ),
                            ]
                        )
                    )
                )
            )
        ),
        onTap: () {
          // Navigator.pop(context);
        }
    );
  }
}